﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TickManager
{
    public class TickManager
    {       
        private static int MonthToDays(int year, int month)
        {
            int add = 0;
            int result = 0;

            if ((year % 4 == 0) && ((year % 100 != 0) || ((year % 100 == 0) && (year % 400 == 0)))) add++;

            switch (month)
            {
                case 0: return 0;
                case 1: result = 31; break;
                case 2: result = 59; break;
                case 3: result = 90; break;
                case 4: result = 120; break;
                case 5: result = 151; break;
                case 6: result = 181; break;
                case 7: result = 212; break;
                case 8: result = 243; break;
                case 9: result = 273; break;
                case 10: result = 304; break;
                case 11: result = 334; break;
                case 12: result = 365; break;
            }
            if (month > 1) result += add;
            return result;
        }

        private static Int64 DateToTicks(int year, int month, int day)
        {
            int a = (year - 1) * 365;
            int b = (year - 1) / 4;
            int c = (year - 1) / 100;
            int d = (a + -c);
            int e = (year - 1) / 400;
            int f = (d + e);
            int monthDays = MonthToDays(year, month);
            int g = (f + monthDays) + day;
            int h = (g - 1);
            return Int64.Parse((h * 864000000000).ToString());
        }

        private static Int64 TimeToTicks(int hour, int minute, int second)
        {
            return Int64.Parse((((hour * 3600) + minute * 60) + second).ToString()) * 10000000;
        }

        private static Int64 MilliseconsToTick(int ms) {
            return Int64.Parse((ms * 10000).ToString());
        }

        public static Int64 GetTick(DateTime date)
        {
            //return DateToTicks(date.Year, date.Month, date.Day) + TimeToTicks(date.Hour, date.Minute, date.Second) + MilliseconsToTick(date.Millisecond);
            return date.Ticks;
        }
    }
}
